//
//  VideonMainViewController.m
//  OfficeEtc
//
//  Created by Wangli on 12-6-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "VideonMainViewController.h"

@interface VideonMainViewController ()

@end

@implementation VideonMainViewController

- (void)viewDidLoad
{
    _sourceType = kSourceVideo;
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    //没有视频的时候，右上角为“添加”;
    //有视频的时候，右上角为“编辑”
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
                                                                                            target:self
                                                                                            action:@selector(addPhotoAction)] autorelease];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (void)dealloc
{
    [super dealloc];
}

#pragma mark - 
#pragma mark Photo Methods
/*
**  myasset:视频资源
**  videoPath:存储视频的路径
*/
-(void)saveVideoFromAssetForURL:(ALAsset *)myasset videoPath:(NSString*)videoPath videoIndex:(NSInteger)videoIndex
{
    ALAssetRepresentation *rep = [myasset defaultRepresentation];

    NSOutputStream *outPutStream = [NSOutputStream outputStreamToFileAtPath:videoPath append:NO];
    [outPutStream open];
    
    NSUInteger bufferSize = 1024*100;
    unsigned char buf[bufferSize];
    NSUInteger writeSize = 0;
    NSUInteger videoSize = [rep size];
    NSError *err = nil;
    while(videoSize != 0)
    {
        NSUInteger readSize = (bufferSize < videoSize)?bufferSize:videoSize;
        [rep getBytes:buf fromOffset: writeSize
               length:readSize error:&err];
        [outPutStream write:buf maxLength:readSize];
        
        videoSize -= readSize;
        writeSize += readSize;
    }
    [outPutStream close];
    
}

- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info {
    
    for(NSDictionary *dict in info)
    {
        
        
        /*
         key:@"SourceAsset", 值类型：ALAsset*(该资源的asset)
         key:@"image_ext", 字符串：资源后缀名，比如mov,mp4等
         
         
         [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]];//视频的URL(NSURL*)
         */
        
    }
    
	[self dismissModalViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark UIImagePickerControllerDelegate

//显示视频预览图像
-(UIImage*)showPreviewImage:(NSURL*)videoURL
{
    
	MPMoviePlayerViewController *videoPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
	videoPlayer.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
    
	UIImage* image = [videoPlayer.moviePlayer thumbnailImageAtTime:0.0 
                                                        timeOption:MPMovieTimeOptionNearestKeyFrame];
	[videoPlayer.moviePlayer stop];
	[videoPlayer release];
	
    return image;
}

// this get called when an image has been chosen from the library or taken from the camera
//
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // NSURL *videoURL = [info valueForKey:UIImagePickerControllerMediaURL];
    //某种方面来说，摄像头拍摄的视频直接文件移动即可，但是移动的时候无进度显示是个纠结的问题
    
    
	[_imagePickerController dismissModalViewControllerAnimated:YES];
	
    
}

////////////////////////////////////
#pragma mark -
#pragma mark VIDEO Methods
-(void)playVideo:(NSURL*)videoURL
{
    MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc]initWithContentURL:videoURL];
    [self presentMoviePlayerViewControllerAnimated:player];
    [player release];
    
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(playbackDidFinish) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:nil];
    
	
}

- (void) playbackDidFinish
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];  
	[self dismissMoviePlayerViewControllerAnimated];
}

@end
